package vip.idiy.anwei_android_iot.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import vip.idiy.anwei_android_iot.R;
import vip.idiy.anwei_android_iot.domin.IconEntity;

/**
 * ClassName IconSelectDialog
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 10:51 2023/4/26
 */
public class IconSelectDialog extends Dialog {
    private final TextView icon_dialog_text_view; // 对话框标题
    private RecyclerView recyclerView; // 图标列表
    private IconSelectAdapter adapter; // 图标列表适配器
    private List<IconEntity> iconList; // 图标列表数据
    private OnIconSelectListener listener; // 图标选择监听器
    public interface OnIconSelectListener {
        void onIconSelect(IconEntity entity); // 图标选择回调
    }
    public IconSelectDialog(@NonNull Context context, String title, OnIconSelectListener listener, List<IconEntity> iconList) {
        super(context, R.style.custom_dialog);
        this.listener = listener;
        this.iconList = iconList;
        View view = LayoutInflater.from(context).inflate(R.layout.layout_icon_dialog, null);
        recyclerView = view.findViewById(R.id.icon_dialog_recycler_view);
        // 标题
        icon_dialog_text_view = view.findViewById(R.id.icon_dialog_text_view);
        // 取消按钮
        view.findViewById(R.id.icon_dialog_my_image_button_cancel).setOnClickListener(v -> dismiss());

        // 设置RecyclerView的布局管理器为网格布局，每行7个
        recyclerView.setLayoutManager(new GridLayoutManager(context, 7));
        // 添加RecyclerView的Item间距
        recyclerView.addItemDecoration(new SpaceItemDecoration(5));
        adapter = new IconSelectAdapter(iconList, (entity) -> {
            if (listener != null) {
                listener.onIconSelect(entity);
            }
            dismiss();
        });
        recyclerView.setAdapter(adapter);
        icon_dialog_text_view.setText(title);
        setContentView(view);
    }
    private static class IconSelectAdapter extends RecyclerView.Adapter<IconSelectViewHolder> {
        private List<IconEntity> iconList; // 图标列表数据
        private OnIconClickListener listener; // 图标点击监听器
        public interface OnIconClickListener {
            void onIconClick(IconEntity entity); // 图标点击回调
        }

        //RecyclerView的适配器，用于显示图标列表
        public IconSelectAdapter(List<IconEntity> iconList, OnIconClickListener listener) {
            this.iconList = iconList;
            this.listener = listener;
        }
        @NonNull
        @Override
        public IconSelectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // 加载图标列表项的布局文件
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_icon_item, null);
            return new IconSelectViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull IconSelectViewHolder holder, int position) {
            // 获取当前位置的图标实体
            IconEntity entity = iconList.get(position);
            // 设置图标视图的图片资源和图标名称视图的文本
            holder.iconView.setImageResource(entity.getIcon());
            holder.nameView.setText(entity.getIconName());
            // 设置图标列表项的点击事件
            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onIconClick(entity);
                }
            });
        }
        @Override
        public int getItemCount() {
            // 返回图标列表的长度
            return iconList.size();
        }
    }
    private static class IconSelectViewHolder extends RecyclerView.ViewHolder {
        private ImageView iconView; // 图标视图
        private TextView nameView; // 图标名称视图
        public IconSelectViewHolder(@NonNull View itemView) {
            super(itemView);
            iconView = itemView.findViewById(R.id.icon_image_view);
            nameView = itemView.findViewById(R.id.name_text_view);
        }
    }
    public static IconSelectDialog newInstance(Context context, String title, OnIconSelectListener listener, List<IconEntity> iconList) {
        return new IconSelectDialog(context, title,listener, iconList);
    }
}

